﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Client
{
    /// <summary>
    /// canvas scaler 运行时调整
    /// </summary>
    [ExecuteInEditMode]
    public class CanvasScalerInGame : MonoBehaviour
    {
        private CanvasScaler cs;
        private bool m_ScaleFlag;

        void Start()
        {
            cs = GetComponent<CanvasScaler>();

            m_ScaleFlag = Screen.width * Global.DesignHeight < Screen.height * Global.DesignWidth;
            cs.matchWidthOrHeight = m_ScaleFlag ? 0 : 1;
        }

#if UNITY_EDITOR
        void Update()
        {
            bool scalerFlag = Screen.width * 9 < Screen.height * 16;
            if (m_ScaleFlag != scalerFlag)
            {
                m_ScaleFlag = scalerFlag;
                cs.matchWidthOrHeight = scalerFlag ? 0 : 1;
            }
        }
#endif
    }
}
